package com.learn.excel.core.util;

import cn.hutool.core.bean.BeanUtil;
import com.learn.excel.core.common.cache.CacheHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.*;

/**
 * @author: 杜海强
 * @description:
 * @date: 2021/5/12 11:45
 */
@Slf4j
public class BeanUtils {

    public static <T, R> void copyBean(T source, R target) {
        org.springframework.beans.BeanUtils.copyProperties(source, target);
    }

    public static <T, R> void copyList(List<T> source, List<R> target, Class<R> rClass) {
        for (T t : source) {
            R r = null;
            try {
                r = rClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            copyBean(t, r);
            target.add(r);
        }
    }

    /**
     * 复制对象属性
     *
     * @param source 原对象
     * @param target 目标对象
     * @return
     */
    public static <T1, T2> T2 copyProperties(T1 source, Class<T2> target) {
        T2 returnModel = null;
        try {
            returnModel = target.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("create object " + target.getName() + " failure");
        }
        org.springframework.beans.BeanUtils.copyProperties(source, returnModel);
        return returnModel;
    }

    public static <T1, T2> List<T2> copyPropertiesList(List<T1> source, Class<T2> target) {
        List<T2> returnList = new ArrayList<>();
        for (T1 t : source) {
            T2 returnModel = null;
            try {
                returnModel = target.newInstance();
            } catch (Exception e) {
                throw new RuntimeException("create object " + target.getName() + " failure");
            }
            org.springframework.beans.BeanUtils.copyProperties(t, returnModel);
            returnList.add(returnModel);
        }
        return returnList;
    }

    /***
     * 为对象设置项目ID
     * @param obj
     * @return
     */
    public static void setProjectId(Object obj) {
        Integer projectId = CacheHandler.getProjectId();
        Map<String, Object> map = BeanUtil.beanToMap(obj);
        if (map.containsKey("projectId")) {
            map.put("projectId", projectId);
            BeanUtil.fillBeanWithMap(map, obj, true);
        }
    }

    /**
     * 复制对象属性, 忽略 null 值
     *
     * @param source 原对象
     * @param target 目标对象
     * @return
     */
    public static void copyPropertiesIgnoreNull(Object source, Object target) {
        String[] ignoreProperties = getNullPropertyNames(source);
        org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties);
    }

    /***
     * 获取对象中属性值为null的属性数组
     * @param source
     * @return
     */
    private static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

}
